import React, { useEffect } from 'react'
// import type { FormProps } from 'antd';
import { Button, Card, Form, Input, message, Space } from 'antd';
import { useNavigate, useParams } from 'react-router-dom';
import { addHospitalSetList, getHospitalSetById, updateHosoplistSetList } from '@/api/hospital/hospitalSet';

type FieldType = {
  username?: string;
  password?: string;
  remember?: string;
};


export default function AddUpdate() {
    let navigate = useNavigate()
    let {id} = useParams() // Huoqu参数列表数据
    const [form] = Form.useForm()
   
    useEffect(() => {
       
        id && getHospSetById() // 只有当id存在的时候才进行更新操作
    }, [])
    // 保存添加医院列表数据
    const onFinish = async () => {
        let data = form.getFieldsValue()
        
        try{
            if(id) {
                data.id = id
                await updateHosoplistSetList(data)
                message.success('更新数据成功')
                navigate('/syt/hospital/hospitalSet')

            } else{
                await addHospitalSetList(data)
                message.success('添加数据成功')
                navigate('/syt/hospital/hospitalSet')

            }
           
        }catch(err){
            message.error('操作失败')
        }
        
    }
    // 编辑医院详情数
    const getHospSetById = async ()=> {
        let res =await getHospitalSetById(id as string)
        console.log(res);
        form.setFieldsValue(res) // 使用form.setFildsValue() 函数，将请求过来的数据传递进去
        
    }
  return (
    <div>
      <Card>
      <Form
        name="basic"
        form={form}
        labelCol={{ span: 2 }}
        wrapperCol={{ span: 20 }}
        onFinish={onFinish}
        autoComplete="off"
    >
        <Form.Item<FieldType>
            label="医院名称"
            name="hosname"
            rules={[{ required: true, message: '请输入医院名称' }]}
        >
        <Input />
        </Form.Item>

        <Form.Item<FieldType>
            label="医院编号"
            name="hoscode"
            rules={[{ required: true, message: '请输入医院编号' }]}
        >
        <Input/>
        </Form.Item>

        <Form.Item<FieldType>
            label="api路径"
            name="apiUrl"
            rules={[{ required: true, message: '请输入api基础路径' }]}
        >
        <Input/>
        </Form.Item>

        <Form.Item<FieldType>
            label="联系人姓名"
            name="contactsName"
            rules={[{ required: true, message: '请输入联系人姓名' }]}
        >
        <Input/>
        </Form.Item>

        <Form.Item<FieldType>
            label="联系人手机"
            name="contactsPhone"
            rules={[{ required: true, message: '请输入联系人手机号' }]}
        >
        <Input/>
        </Form.Item>

        <Form.Item wrapperCol={{offset: 2,span: 16}}>
            <Space>
                <Button className='bor-r' type="primary" htmlType="submit">保存</Button>
                <Button className='bor-r' onClick={() => {navigate(-1)}}>返回</Button>
            </Space>
        
        </Form.Item>
    </Form>
      </Card>
    </div>
  )
}
